package com.aixin.project.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aixin.project.data.Expert;
import com.aixin.project.handler.ExpertHandler;

public class ExpertPaginServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String page = request.getParameter("page");
		if (page == null || page.trim().length() == 0)
			page = "1";
		int curpage = Integer.parseInt(page);
		int maxpage = ExpertHandler.getMaxpage();
		StringBuffer sb=new StringBuffer();
		sb.append("[{maxpage:\""+maxpage+"\",curpage:\""+curpage+"\"},");
		List<Expert> expertList = ExpertHandler.expertList(curpage);
	
		if(expertList!=null){
			for(int i=0;i<expertList.size();i++){
			
				Expert ex=expertList.get(i);
				if(i==(expertList.size()-1)){
					sb.append("{id:\""+ex.getId()+"\",name:\""+ex.getName()+"\",description:\""+ex.shortDesc(40)+"\",post:\""+ex.getPost()+"\"}");
				}else{
				   sb.append("{id:\""+ex.getId()+"\",name:\""+ex.getName()+"\",description:\""+ex.shortDesc(40)+"\",post:\""+ex.getPost()+"\"},");
				}
			}
			sb.append("]");
			
			String content=sb.toString();
			if(content.indexOf(",]")!=-1){
				content=content.replace(",]", "");
			}
			
			
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out=response.getWriter();	
			out.println(content);
			
			out.close();
			
		}
		
	
	}
}
